﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using System.Data;
using Runda.EShop.Bll;

namespace Runda.EShop.SaaS.OrderManage
{
    public partial class ComplainList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EnumDescAttribute[] enums = EnumDescAttribute.GetFieldTexts(typeof(EnumHelper.OrderComplaintStatus));
                foreach (EnumDescAttribute enun in enums)
                {
                    ddlStatus.Items.Add(new ListItem(enun.EnumDisplayText, enun.EnumDisplayText));
                }
                ddlStatus.Items.Insert(0, new ListItem("全部", ""));

                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(Page_Load);
            AspNetPager1.PageChanged += new EventHandler(AspNetPager1_PageChanged);
            lnkbtnQuery.Click += new EventHandler(lnkbtnQuery_Click);
            gvOrderList.RowDataBound += new GridViewRowEventHandler(gvOrderList_RowDataBound);
            gvOrderList.RowCommand += new GridViewCommandEventHandler(gvOrderList_RowCommand);
        }

        void gvOrderList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Guid ComplainID = new Guid(e.CommandArgument.ToString());
            BizOrderComplaintReplyBll bll = new BizOrderComplaintReplyBll();
            if (e.CommandName.ToLower() == "close")
            {
                bll.UpdateComplaintStatus(ComplainID, EnumHelper.OrderComplaintStatus.Close);
            }

            if (e.CommandName.ToLower() == "process")
            {
                bll.UpdateComplaintStatus(ComplainID, EnumHelper.OrderComplaintStatus.Processing);
            }

            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        void gvOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                BizOrderComplaintEntity entity = e.Row.DataItem as BizOrderComplaintEntity;
                HyperLink lnkView = e.Row.FindControl("lnkView") as HyperLink;
                LinkButton lnkbtnProcess = e.Row.FindControl("lnkbtnProcess") as LinkButton;
                LinkButton lnkbtnClose = e.Row.FindControl("lnkbtnClose") as LinkButton;
                if (entity.Status == EnumDescAttribute.GetFieldText(EnumHelper.OrderComplaintStatus.Pending))
                {
                    lnkbtnClose.Visible = false;
                }
                if (entity.Status == EnumDescAttribute.GetFieldText(EnumHelper.OrderComplaintStatus.Processing))
                {
                    lnkbtnProcess.Visible = false;
                }
                if (entity.Status == EnumDescAttribute.GetFieldText(EnumHelper.OrderComplaintStatus.Close))
                {
                    lnkbtnProcess.Visible = false;
                    lnkbtnClose.Visible = false;
                }
            }
        }

        void lnkbtnQuery_Click(object sender, EventArgs e)
        {
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }

        void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            if (ddlStatus.SelectedValue != "")
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[Status]";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@Status";
                parm.ParameterValue = ddlStatus.SelectedValue;
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            int RecordCount = 0;
            List<BizOrderComplaintEntity> dt = new OrderBll().GetAll<BizOrderComplaintEntity>(parms, "CreatedAt DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvOrderList.DataSource = dt;
            gvOrderList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }
    }
}